<template>
  <div class="layui-container fly-marginTop">
    <div class="fly-panel fly-panel-user" pad20>
      <div class="layui-tab layui-tab-brief" lay-filter="user">
        <ul class="layui-tab-title">
          <li class="layui-this">登入</li>
          <li>
            <router-link :to="{ name: 'reg' }">注册</router-link>
          </li>
        </ul>
        <div
          class="layui-form layui-tab-content"
          id="LAY_ucm"
          style="padding: 20px 0;"
        >
          <div class="layui-tab-item layui-show">
            <div class="layui-form layui-form-pane">
              <validation-observer ref="observer" v-slot="{validate}">
              <form method="post">
                <div class="layui-form-item">
                  <label class="layui-form-label">用户名</label>
                  <ValidationProvider
                    rules="required|email"
                    v-slot="{ errors }"
                  >
                    <div class="layui-input-inline">
                      <input
                        type="text"
                        name="username"
                        v-model.trim="username"
                        placeholder="请输入标题"
                        autocomplete="off"
                        class="layui-input"
                      />
                    </div>
                    <div class="layui-form-mid form-error">{{ errors[0] }}</div>
                  </ValidationProvider>
                </div>
                <div class="layui-form-item">
                  <label class="layui-form-label">密码</label>
                  <ValidationProvider
                    rules="required|min:6"
                    v-slot="{ errors }"
                  >
                    <div class="layui-input-inline">
                      <input
                        type="password"
                        name="password"
                        v-model="password"
                        placeholder="请输入标题"
                        autocomplete="off"
                        class="layui-input"
                      />
                    </div>
                    <div class="layui-form-mid form-error">{{ errors[0] }}</div>
                  </ValidationProvider>
                </div>
                <div class="layui-form-item">
                  <label class="layui-form-label">验证码</label>
                  <ValidationProvider
                    rules="required|length:4"
                    v-slot="{ errors }"
                  >
                    <div class="layui-input-inline">
                      <input
                        type="text"
                        name="code"
                        v-model="code"
                        placeholder="请输入标题"
                        autocomplete="off"
                        class="layui-input"
                      />
                    </div>
                    <span class="svg" @click="getCaptcha()" v-html="svg"></span>
                    <div class="form-error">{{ errors[0] }}</div>
                  </ValidationProvider>
                </div>
                <div class="layui-form-item">
                  <button class="layui-btn" type="button" @click="validate().then(submit)">
                    立即登录
                  </button>
                  <span style="padding-left:20px;">
                    <router-link :to="{ name: 'forget' }"
                      >忘记密码？</router-link
                    >
                  </span>
                </div>
                <div class="layui-form-item fly-form-app">
                  <span>或者使用社交账号登入</span>
                  <a
                    href
                    onclick="layer.msg('正在通过QQ登入', {icon:16, shade: 0.1, time:0})"
                    class="iconfont icon-qq"
                    title="QQ登入"
                  ></a>
                  <a
                    href
                    onclick="layer.msg('正在通过微博登入', {icon:16, shade: 0.1, time:0})"
                    class="iconfont icon-weibo"
                    title="微博登入"
                  ></a>
                </div>
              </form>
              </validation-observer>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCode, login } from '@/api/login.js'
import { ValidationProvider, ValidationObserver } from 'vee-validate'
import { v4 as uuidv4 } from 'uuid'
export default {
  name: 'login',
  data () {
    return {
      svg: '',
      password: '',
      username: '',
      code: ''
    }
  },
  components: {
    ValidationProvider,
    ValidationObserver
  },
  async mounted () {
    let sid = ''
    if (localStorage.getItem('sid')) {
      sid = localStorage.getItem('sid')
    } else {
      sid = uuidv4()
      localStorage.setItem('sid', sid)
    }
    this.$store.commit('setSid', sid)
    this.getCaptcha()
  },
  methods: {
    async submit () {
      const valid = await this.$refs.observer.validate()
      if (!valid) {
        return
      }
      const res = await login({
        username: this.username,
        password: this.password,
        sid: this.$store.state.sid,
        code: this.code
      })
      if (res.code !== 200) {
        this.$alert(res.msg)
      } else {
        res.data.username = this.username
        this.$store.commit('setUserInfo', res.data)
        this.$store.commit('setLoginStatus', true)
        this.$store.commit('setToken', res.token)
        this.$router.push({
          name: 'index'
        }, () => {})
      }
    },
    async getCaptcha () {
      const sid = this.$store.state.sid
      const res = await getCode(sid)

      if (res.code === 200) {
        this.svg = res.data
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.layui-container {
  background: #f2f2f2;
  .layui-container {
    background: #ffffff;
  }
  input {
    width: 190px;
  }
  .imooc-link {
    margin-left: 10px;
    &:hover {
      color: #009688;
    }
  }

}
</style>
